# Makefile for JPEG Histogram SDL Visualizer
# 
# 用于编译JPEG图像直方图SDL可视化工具的Makefile

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
LDFLAGS = -ljpeg -lSDL2 -lSDL2_ttf -lm

# 检测系统和包管理器
UNAME_S := $(shell uname -s 2>/dev/null || echo "Windows")
PKG_CONFIG := $(shell which pkg-config 2>/dev/null)

# 根据系统设置包配置
ifeq ($(PKG_CONFIG),)
    # 没有pkg-config，使用默认设置
    JPEG_CFLAGS = 
    JPEG_LIBS = -ljpeg
    SDL2_CFLAGS = 
    SDL2_LIBS = -lSDL2 -lSDL2_ttf
else
    # 尝试使用pkg-config
    JPEG_CFLAGS := $(shell pkg-config --cflags libjpeg 2>/dev/null || echo "")
    JPEG_LIBS := $(shell pkg-config --libs libjpeg 2>/dev/null || echo "-ljpeg")
    SDL2_CFLAGS := $(shell pkg-config --cflags sdl2 SDL2_ttf 2>/dev/null || echo "")
    SDL2_LIBS := $(shell pkg-config --libs sdl2 SDL2_ttf 2>/dev/null || echo "-lSDL2 -lSDL2_ttf")
endif

# 添加相关的编译和链接选项
CFLAGS += $(JPEG_CFLAGS) $(SDL2_CFLAGS)
LDFLAGS = $(JPEG_LIBS) $(SDL2_LIBS) -lm

# 目标文件和源文件
TARGET = jpg_histogram_sdl
SRCDIR = .
OBJDIR = obj
SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

# 默认目标
.PHONY: all
all: check-deps $(TARGET)

# 检查依赖库
.PHONY: check-deps
check-deps:
	@echo "检查依赖库..."
	@echo "编译选项: $(CFLAGS)"
	@echo "链接选项: $(LDFLAGS)"
	@echo ""
	@echo "检查JPEG库..."
	@if ! echo '#include <jpeglib.h>' | $(CC) -E $(JPEG_CFLAGS) - >/dev/null 2>&1; then \
		echo "❌ 错误: 未找到libjpeg开发库"; \
		echo "请安装JPEG库:"; \
		echo "  Ubuntu/Debian: sudo apt-get install libjpeg-dev"; \
		echo "  CentOS/RHEL:   sudo yum install libjpeg-devel"; \
		echo "  macOS:         brew install jpeg"; \
		exit 1; \
	fi
	@echo "✅ JPEG库检查通过"
	@echo ""
	@echo "检查SDL2库..."
	@if ! echo '#include <SDL2/SDL.h>' | $(CC) -E $(SDL2_CFLAGS) - >/dev/null 2>&1; then \
		echo "❌ 错误: 未找到SDL2开发库"; \
		echo "请安装SDL2库:"; \
		echo "  Ubuntu/Debian: sudo apt-get install libsdl2-dev libsdl2-ttf-dev"; \
		echo "  CentOS/RHEL:   sudo yum install SDL2-devel SDL2_ttf-devel"; \
		echo "  macOS:         brew install sdl2 sdl2_ttf"; \
		exit 1; \
	fi
	@echo "✅ SDL2库检查通过"
	@echo ""
	@echo "检查SDL2_ttf库..."
	@if ! echo '#include <SDL2/SDL_ttf.h>' | $(CC) -E $(SDL2_CFLAGS) - >/dev/null 2>&1; then \
		echo "❌ 错误: 未找到SDL2_ttf开发库"; \
		echo "请安装SDL2_ttf库:"; \
		echo "  Ubuntu/Debian: sudo apt-get install libsdl2-ttf-dev"; \
		echo "  CentOS/RHEL:   sudo yum install SDL2_ttf-devel"; \
		echo "  macOS:         brew install sdl2_ttf"; \
		exit 1; \
	fi
	@echo "✅ SDL2_ttf库检查通过"
	@echo "✅ 所有依赖库检查完成"

# 创建目标目录
$(OBJDIR):
	mkdir -p $(OBJDIR)

# 编译目标程序
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $@ $(LDFLAGS)
	@echo "编译完成: $(TARGET)"

# 编译目标文件
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

# 清理编译产物
.PHONY: clean
clean:
	rm -rf $(OBJDIR) $(TARGET) $(TARGET).exe
	@echo "清理完成"

# 安装（可选）
.PHONY: install
install: $(TARGET)
	@echo "安装到 /usr/local/bin (需要sudo权限):"
	sudo cp $(TARGET) /usr/local/bin/

# 运行测试（需要测试图片）
.PHONY: test
test: $(TARGET)
	@if [ -f "test.jpg" ]; then \
		echo "运行测试..."; \
		./$(TARGET) test.jpg -t 3000; \
	elif [ -f "../test.jpg" ]; then \
		echo "运行测试..."; \
		./$(TARGET) ../test.jpg -t 3000; \
	else \
		echo "错误: 需要test.jpg文件进行测试"; \
		echo "请将JPEG测试文件重命名为test.jpg"; \
	fi

# 交互式测试
.PHONY: test-interactive
test-interactive: $(TARGET)
	@if [ -f "test.jpg" ]; then \
		echo "运行交互式测试（按ESC退出）..."; \
		./$(TARGET) test.jpg; \
	elif [ -f "../test.jpg" ]; then \
		echo "运行交互式测试（按ESC退出）..."; \
		./$(TARGET) ../test.jpg; \
	else \
		echo "错误: 需要test.jpg文件进行测试"; \
		echo "请将JPEG测试文件重命名为test.jpg"; \
	fi

# 调试编译
.PHONY: debug
debug: CFLAGS += -DDEBUG -g3 -O0
debug: clean $(TARGET)
	@echo "调试版本编译完成"

# 发布编译
.PHONY: release
release: CFLAGS += -DNDEBUG -O3
release: clean $(TARGET)
	strip $(TARGET) 2>/dev/null || true
	@echo "发布版本编译完成"

# 显示帮助信息
.PHONY: help
help:
	@echo "可用的make目标:"
	@echo "  all              - 编译程序 (默认)"
	@echo "  check-deps       - 检查依赖库"
	@echo "  clean            - 清理编译产物"
	@echo "  debug            - 编译调试版本"
	@echo "  release          - 编译发布版本"
	@echo "  test             - 运行自动测试 (需要test.jpg)"
	@echo "  test-interactive - 运行交互式测试"
	@echo "  install          - 安装到系统"
	@echo "  install-deps     - 安装依赖库"
	@echo "  help             - 显示此帮助信息"
	@echo "  info             - 显示编译信息"

# 显示编译信息
.PHONY: info
info:
	@echo "编译器: $(CC)"
	@echo "编译选项: $(CFLAGS)"
	@echo "链接选项: $(LDFLAGS)"
	@echo "源文件: $(SOURCES)"
	@echo "目标文件: $(OBJECTS)"
	@echo "目标程序: $(TARGET)"
	@echo "系统: $(UNAME_S)"

# 安装依赖库的快捷命令
.PHONY: install-deps
install-deps:
	@echo "尝试安装依赖库..."
	@if command -v apt-get >/dev/null 2>&1; then \
		echo "检测到Debian/Ubuntu系统"; \
		sudo apt-get update && sudo apt-get install -y libjpeg-dev libsdl2-dev libsdl2-ttf-dev pkg-config; \
	elif command -v yum >/dev/null 2>&1; then \
		echo "检测到CentOS/RHEL系统"; \
		sudo yum install -y libjpeg-devel SDL2-devel SDL2_ttf-devel pkgconfig; \
	elif command -v dnf >/dev/null 2>&1; then \
		echo "检测到Fedora系统"; \
		sudo dnf install -y libjpeg-devel SDL2-devel SDL2_ttf-devel pkgconfig; \
	elif command -v brew >/dev/null 2>&1; then \
		echo "检测到macOS系统"; \
		brew install jpeg sdl2 sdl2_ttf pkg-config; \
	else \
		echo "无法自动安装，请手动安装以下库:"; \
		echo "- libjpeg开发库"; \
		echo "- SDL2开发库"; \
		echo "- SDL2_ttf开发库"; \
	fi

# 创建字体目录和下载字体（可选）
.PHONY: setup-fonts
setup-fonts:
	@echo "设置字体..."
	@mkdir -p fonts
	@echo "字体目录已创建: ./fonts/"
	@echo "提示: 您可以将TTF字体文件放入fonts/目录中"
	@echo "或者程序会自动查找系统字体"